package com.xbongbong.saas.model.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.elasticsearch.model.PaasEsModel;
import com.xbongbong.paas.elasticsearch.pojo.dto.InsertDTO;
import com.xbongbong.paas.elasticsearch.util.EsHelper;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.FlowStatusEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.help.subform.TransferSubFormHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.domain.dao.ContactUserDao;
import com.xbongbong.saas.domain.entity.ContactUserEntity;
import com.xbongbong.saas.domain.entity.UserTeamEntity;
import com.xbongbong.saas.enums.business.ContactEnum;
import com.xbongbong.saas.enums.subform.ContactTeamEnum;
import com.xbongbong.saas.enums.subform.PaymentSheetTeamEnum;
import com.xbongbong.saas.model.ContactUserModel;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;

/**
 * @author feng.zheng
 * @version v1.0
 * @date 2019/1/24 15:45
 * @since v1.0
 */
@Service("contactUserModel")
public class ContactUserModelImpl implements ContactUserModel {
    @Resource
    private ContactUserDao contactUserDao;
    @Resource
    private PaasEsModel paasEsModel;
    @Resource
    private EsHelper esHelper;
    @Resource
    private TransferSubFormHelper transferSubFormHelper;

    @Override
    public Integer insert(ContactUserEntity contactUserEntity) throws XbbException {
        long now = DateTimeUtil.getInt();
        contactUserEntity.setAddTime(now);
        contactUserEntity.setUpdateTime(now);
        Integer result = contactUserDao.insert(contactUserEntity);
        PaasFormDataEntity paasFormDataEntity = transferSubFormHelper.transferUserTeamToFormData(IndexTypeEnum.IDX_SAAS_CONTACT_TEAM, contactUserEntity);
        InsertDTO saveDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_CONTACT_TEAM);
        paasEsModel.insert(saveDTO);
//        paasEsModel.insert(contactUserEntity,IndexTypeEnum.IDX_SAAS_CONTACT_TEAM,contactUserEntity.getCorpid(),contactUserEntity.getId(),contactUserEntity.getDataId());
        return result;
    }

    @Override
    public Integer update(ContactUserEntity contactUserEntity) throws XbbException {
        long now = DateTimeUtil.getInt();
        contactUserEntity.setUpdateTime(now);
        Integer result = contactUserDao.update(contactUserEntity);
        PaasFormDataEntity paasFormDataEntity = transferSubFormHelper.transferUserTeamToFormData(IndexTypeEnum.IDX_SAAS_CONTACT_TEAM, contactUserEntity);
        InsertDTO saveDTO = paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_CONTACT_TEAM);
        paasEsModel.update(saveDTO);
//        paasEsModel.update(contactUserEntity,IndexTypeEnum.IDX_SAAS_CONTACT_TEAM,contactUserEntity.getCorpid(),contactUserEntity.getId(),contactUserEntity.getDataId());
        return result;
    }

    /**
     * 根据客户id和userId删除联系人团队
     *
     * @param corpid
     * @param customerId
     * @param userId
     * @return java.lang.Integer
     * @author feng.zheng
     * @date 2019/3/10 15:13
     */
    @Override
    public Integer deleteByUserIdAndCustomerId(String corpid, Long customerId, String userId) throws XbbException{
        Integer result = contactUserDao.deleteByUserIdAndCustomerId(corpid, customerId, userId);
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid",corpid);
        map.put("del",0);
        map.put("customerId",customerId);
        map.put("userId",userId);
        List<ContactUserEntity> contactUserEntityList =  findEntitys(map);
        for (ContactUserEntity entity : contactUserEntityList){
            paasEsModel.delete(entity.getId(), corpid, IndexTypeEnum.IDX_SAAS_CONTACT_TEAM,entity.getDataId());
        }
        return result;
    }

    @Override
    public List<ContactUserEntity> findEntitys(Map<String, Object> param) {
        List<ContactUserEntity> list = contactUserDao.findEntitys(param);
        List<ContactUserEntity> retList = new ArrayList<ContactUserEntity>();
        Set<String> checkSet = new HashSet<String>();
        //去重
        for (ContactUserEntity contactUserEntity : list) {
            String key = contactUserEntity.getUserId() + "_" + contactUserEntity.getDataId() + "_"
                    + contactUserEntity.getIsMain() + "_" + contactUserEntity.getCorpid();

            if (checkSet.contains(key)) {
                continue;
            }
            retList.add(contactUserEntity);
            checkSet.add(key);
        }

        return retList;
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return contactUserDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public Integer save(ContactUserEntity contactUserEntity) throws XbbException {
        if (contactUserEntity.getId() == null || contactUserEntity.getId().equals(0L)) {
            return insert(contactUserEntity);
        }
        return update(contactUserEntity);
    }

    @Override
    public ContactUserEntity getByKey(Long key, String corpid) {
        return contactUserDao.getByKey(key, corpid);
    }

    @Override
    public Integer deleteBatch(List<Long> list, String corpid) throws XbbException{
        return contactUserDao.deleteBatch(list, corpid, DelEnum.DELETE.getDel());
    }

    @Override
    public void deleteBatch(String corpid, List<ContactUserEntity> list, WriteRequest.RefreshPolicy refreshPolicy, Integer del) throws XbbException {
        List<Long> idList = new ArrayList<>();
        List<InsertDTO> insertDTOList = new ArrayList<>();
        list.forEach(item->{
            idList.add(item.getId());
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CONTACT_TEAM.getType() + "_" + corpid + "_" + item.getId());
            insertDTO.setParent(corpid + "_" + item.getDataId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            map.put("del", del);
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CONTACT_TEAM);
            insertDTOList.add(insertDTO);
        });
        contactUserDao.deleteBatch(idList, corpid, del);
    }

    @Override
    public Integer updateBatch(List<ContactUserEntity> list, String corpid) throws XbbException {
        Integer result = contactUserDao.updateBatch(list, corpid);
        List<InsertDTO> insertList = new ArrayList<>();
        for (ContactUserEntity entity : list){
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CONTACT_TEAM.getType() + "_" + corpid + "_" + entity.getId());
            insertDTO.setParent(corpid + "_" + entity.getDataId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if (Objects.nonNull(entity.getDel())) {
                map.put("del", entity.getDel());
            }
            map.put("updateTime", DateTimeUtil.getInt());
            JSONObject object = new JSONObject();
            if (Objects.nonNull(entity.getIsMain())) {
                object.put(ContactTeamEnum.IS_MAIN.getDataAttr(), entity.getIsMain());
            }
            if (Objects.nonNull(entity.getDataId())) {
                object.put(ContactTeamEnum.DATA_ID.getDataAttr(), entity.getDataId());
            }
            if (Objects.nonNull(entity.getUserId())) {
                object.put(ContactTeamEnum.USER_ID.getDataAttr(), entity.getUserId());
            }
            if (Objects.nonNull(entity.getUserName())) {
                object.put(ContactTeamEnum.USER_NAME.getDataAttr(), entity.getUserName());
            }
            if (Objects.nonNull(entity.getUserAvatar())) {
                object.put(ContactTeamEnum.USER_AVATAR.getDataAttr(), entity.getUserAvatar());
            }
            if (Objects.nonNull(entity.getCustomerId())) {
                object.put(ContactTeamEnum.CUSTOMER_ID.getDataAttr(), entity.getCustomerId());
            }
            if (Objects.nonNull(entity.getPosition())) {
                object.put(ContactTeamEnum.POSITION.getDataAttr(), entity.getPosition());
            }
            map.put(StringConstant.JSON_DATA, object);
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CONTACT_TEAM);
            insertList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertList);
        return result;
    }

    @Override
    public Integer deleteBatchByCustomerAndContact(Long customerId, Long contactId, String corpid) throws XbbException {
        if (customerId == null || corpid == null || contactId == null) {
            return 0;
        }
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("customerId", customerId);
        param.put("contactId", contactId);
        param.put("corpid", corpid);
        List<ContactUserEntity> contactUserList = findEntitys(param);
        List<Long> list = new ArrayList<>();
        for (ContactUserEntity contactUserEntity : contactUserList) {
            list.add(contactUserEntity.getId());
        }
        if (list.isEmpty()) {
            return 1;
        }
        Integer result = contactUserDao.deleteBatch(list, corpid, DelEnum.DELETE.getDel());
        for (ContactUserEntity entity :contactUserList){
            paasEsModel.delete(entity.getId(), corpid, IndexTypeEnum.IDX_SAAS_CONTACT_TEAM,entity.getDataId());
        }
        return result;
    }

    @Override
    public void disbandedTeamByCustomerId(String corpid, List<Long> customerIdIn) throws XbbException{
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword",corpid));
        boolQueryBuilder.filter(termsQuery(ContactEnum.getEsAttr4Keyword(ContactEnum.CUSTOMER_NAME),customerIdIn));
        boolQueryBuilder.filter(termsQuery(FieldTypeEnum.FLOW_STATUS.getAlias(), Arrays.asList(FlowStatusEnum.NOT_NEED_APPROVAL.getType(), FlowStatusEnum.PASS.getType())));
        List<PaasFormDataEntity> paasFormDataEntityList = esHelper.findByScroll(IndexTypeEnum.IDX_SAAS_CONTACT,boolQueryBuilder,PaasFormDataEntity.class,Collections.singletonList(FieldTypeEnum.DATAID.getAlias()));
        if (paasFormDataEntityList.isEmpty()){
            return;
        }
        List<Long> idList = new ArrayList<>();
        for (PaasFormDataEntity entity : paasFormDataEntityList){
            idList.add(entity.getDataId());
        }
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid",corpid);
        map.put("del",0);
        map.put("dataIdIn",idList);
        List<ContactUserEntity> contractUserEntityList = findEntitys(map);
        contactUserDao.deleteBatchByContactIdIn(idList,corpid);
        for (ContactUserEntity entity : contractUserEntityList){
            paasEsModel.delete(entity.getId(), corpid, IndexTypeEnum.IDX_SAAS_CONTACT_TEAM,entity.getDataId());
        }
    }

    @Override
    public Integer deleteBatchByContactIdIn(List<Long> contactIdIn, String corpid) throws XbbException{
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid",corpid);
        map.put("del",0);
        map.put("contactIdIn",contactIdIn);
        List<ContactUserEntity> contractUserEntityList = findEntitys(map);
        Integer result = contactUserDao.deleteBatchByContactIdIn(contactIdIn, corpid);
        for (ContactUserEntity entity : contractUserEntityList){
            paasEsModel.delete(entity.getId(), corpid, IndexTypeEnum.IDX_SAAS_CONTACT_TEAM,entity.getDataId());
        }
        return result;
    }

    @Override
    public void batchUpdateByUserIdIn(String corpid, List<Long> dataIdIn, List<UserTeamEntity> teamEntityList, Integer isMain, Integer del) throws XbbException {
        contactUserDao.batchUpdateByIdIn(corpid, dataIdIn, isMain, del);
        List<InsertDTO> insertList = new ArrayList<>();
        for (UserTeamEntity userTeamEntity : teamEntityList) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CONTACT_TEAM.getType() + "_" + corpid + "_" + userTeamEntity.getId());
            insertDTO.setParent(corpid + "_" + userTeamEntity.getDataId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if (Objects.nonNull(isMain)) {
                JSONObject object = new JSONObject();
                object.put(ContactTeamEnum.IS_MAIN.getDataAttr(), isMain);
                map.put(StringConstant.JSON_DATA, object);
            }
            if (Objects.nonNull(del)) {
                map.put(ParameterConstant.DEL, del);
            }
            map.put(FieldTypeEnum.UPDATETIME.getAlias(), DateTimeUtil.getInt());
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CONTACT_TEAM);
            insertList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertList);
    }

    @Override
    public void batchUpdateByUserIdIn(String corpid, List<Long> dataIdIn, List<UserTeamEntity> teamEntityList, Integer isMain, Integer del, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException {
        contactUserDao.batchUpdateByIdIn(corpid, dataIdIn, isMain, del);
        List<InsertDTO> insertList = new ArrayList<>();
        for (UserTeamEntity userTeamEntity : teamEntityList) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CONTACT_TEAM.getType() + "_" + corpid + "_" + userTeamEntity.getId());
            insertDTO.setParent(corpid + "_" + userTeamEntity.getDataId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if (Objects.nonNull(isMain)) {
                JSONObject object = new JSONObject();
                object.put(ContactTeamEnum.IS_MAIN.getDataAttr(), isMain);
                map.put(StringConstant.JSON_DATA, object);
            }
            if (Objects.nonNull(del)) {
                map.put(ParameterConstant.DEL, del);
            }
            map.put(FieldTypeEnum.UPDATETIME.getAlias(), DateTimeUtil.getInt());
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CONTACT_TEAM);
            insertList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertList, refreshPolicy);
    }

    @Override
    public void insertBatch(List<ContactUserEntity> contactUserEntityList, boolean isImport) throws XbbException {
        contactUserDao.insertBatch(contactUserEntityList);

        List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferUserTeamToFormDataList(IndexTypeEnum.IDX_SAAS_CONTACT_TEAM, contactUserEntityList);
        List<InsertDTO> insertDTOList = new ArrayList<>();
        for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
            insertDTOList.add(paasEsModel.getSaveDTO(paasFormDataEntity, IndexTypeEnum.IDX_SAAS_CONTACT_TEAM));
        }
//        for (ContactUserEntity contactUserEntity : contactUserEntityList) {
//            InsertDTO insertDTO = new InsertDTO();
//            insertDTO.setWaitUntil(WriteRequest.RefreshPolicy.NONE);
//            insertDTO.setEsId(contactUserEntity.getCorpid() + "_" + contactUserEntity.getId());
//            insertDTO.setParent(contactUserEntity.getCorpid() + "_" + contactUserEntity.getDataId());
//            insertDTO.setSource(BeanUtil.convertBean2Map(contactUserEntity, false, true));
//            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CONTACT_TEAM);
//            insertDTOList.add(insertDTO);
//        }
        if (isImport) {
            paasEsModel.insertBatch(insertDTOList, WriteRequest.RefreshPolicy.NONE);
        } else {
            paasEsModel.insertBatch(insertDTOList);
        }
    }

    @Override
    public List<UserTeamEntity> findBaseEntitys(Map<String, Object> map) {
        return contactUserDao.findBaseEntitys(map);
    }

    @Override
    public void deleteBatchUser(List<ContactUserEntity> contactUserEntityList, String corpid) throws XbbException {
        List<Long> idList = new ArrayList<>();
        List<InsertDTO> insertDTOList = new ArrayList<>();
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put(StringConstant.DEL,DelEnum.DELETE.getDel());
        map.put(StringConstant.UPDATE_TIME, DateTimeUtil.getInt());
        for (ContactUserEntity entity : contactUserEntityList){
            idList.add(entity.getId());
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CONTACT_TEAM.getType() + "_" + corpid + "_" + entity.getId());
            insertDTO.setParent(corpid + "_" + entity.getDataId());
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CONTACT_TEAM);
            insertDTO.setIndex(IndexTypeEnum.IDX_SAAS_CONTACT_TEAM.getIndex());
            insertDTO.setType(IndexTypeEnum.IDX_SAAS_CONTACT_TEAM.getType());
            insertDTO.setSource(map);
            insertDTOList.add(insertDTO);
        }
        deleteBatch(idList,corpid);
        paasEsModel.updateBatch(insertDTOList);
    }

    @Override
    public List<ContactUserEntity> getListByCustomerId(String corpid, Long customerId) {
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid",corpid);
        map.put("del",DelEnum.NORMAL.getDel());
        map.put("customerId",customerId);
        return findEntitys(map);
    }

    @Override
    public List<ContactUserEntity> getUserListByContactId(String corpid, Long contactId, Integer del) {
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid", corpid);
        map.put("del", del);
        map.put("dataId", contactId);
        return findEntitys(map);
    }

    @Override
    public List<ContactUserEntity> getListByContactId(String corpid, Long contactId, Integer isMain) {
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("corpid", corpid);
        map.put("del", DelEnum.NORMAL.getDel());
        map.put("dataId", contactId);
        map.put("isMain", isMain);
        return findEntitys(map);
    }

    @Override
    public List<ContactUserEntity> findEntityOriginal(Map<String, Object> params) {
        return contactUserDao.findEntitys(params);
    }

    /**
     *
     * @Description: 编辑团队成员标签
     * @param contactUserEntity
     * @return: void
     * @Author: shen.yang
     * @Date: 2022/1/17  14:20
    */
    @Override
    public void editLabel(ContactUserEntity contactUserEntity) throws XbbException {
        updateFormTeam(contactUserEntity);
    }

    /**
     * 更新协同人可编辑权限
     *
     * @param contactUserEntity
     */
    @Override
    public void updateEditFormPer(ContactUserEntity contactUserEntity) throws XbbException {
        updateFormTeam(contactUserEntity);
    }


    private void updateFormTeam(ContactUserEntity contactUserEntity) throws XbbException {
        Long updateTime = DateTimeUtil.getInt();
        contactUserEntity.setUpdateTime(updateTime);
        contactUserDao.update(contactUserEntity);
        InsertDTO insertDTO = new InsertDTO();
        String corpid = contactUserEntity.getCorpid();
        insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CONTACT_TEAM.getType() + "_" + corpid + "_" + contactUserEntity.getId());
        insertDTO.setParent(corpid + "_" + contactUserEntity.getDataId());
        Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put("updateTime", updateTime);
        insertDTO.setSource(map);
        insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CONTACT_TEAM);
        paasEsModel.update(insertDTO);
    }

    /**
     * 批量更新团队的人员为协同人身份以及协同人的可编辑权限
     *
     * @param corpid
     * @param dataIdIn
     * @param teamEntityList
     * @param isMain
     * @param needThrow
     * @throws XbbException
     */
    @Override
    public void batchUpdateCoAndPerByUserIdIn(String corpid, List<Long> idIn, List<UserTeamEntity> teamEntityList, Integer isMain, boolean needThrow) throws XbbException {
        batchUpdateCoAndPerByUserIdIn(corpid, idIn, teamEntityList, isMain, needThrow, WriteRequest.RefreshPolicy.WAIT_UNTIL);
    }

    @Override
    public void batchUpdateCoAndPerByUserIdIn(String corpid, List<Long> idIn, List<UserTeamEntity> teamEntityList, Integer isMain, boolean needThrow, WriteRequest.RefreshPolicy refreshPolicy) throws XbbException {
        Integer formPermission = teamEntityList.get(BasicConstant.ZERO).getFormPermission();
        contactUserDao.batchUpdateCoAndPerByUserIdIn(corpid, idIn, isMain, formPermission);
        List<InsertDTO> insertList = new ArrayList<>();
        for (UserTeamEntity userTeamEntity : teamEntityList) {
            InsertDTO insertDTO = new InsertDTO();
            insertDTO.setEsId(IndexTypeEnum.IDX_SAAS_CONTACT_TEAM.getType() + "_" + corpid + "_" + userTeamEntity.getId());
            insertDTO.setParent(corpid + "_" + userTeamEntity.getDataId());
            Map<String, Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            if (Objects.nonNull(isMain)) {
                JSONObject object = new JSONObject();
                object.put(PaymentSheetTeamEnum.IS_MAIN.getDataAttr(), isMain);
                map.put(StringConstant.JSON_DATA, object);
            }
            map.put(FieldTypeEnum.UPDATETIME.getAlias(), DateTimeUtil.getInt());
            insertDTO.setSource(map);
            insertDTO.setIndexTypeEnum(IndexTypeEnum.IDX_SAAS_CONTACT_TEAM);
            insertList.add(insertDTO);
        }
        paasEsModel.updateBatch(insertList, refreshPolicy, needThrow);
    }



    @Override
    public void disbandedTeamByDataId(String corpid, List<Long> dataIdList) throws XbbException {
        Map<String,Object> map = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        map.put(ParameterConstant.CORPID, corpid);
        map.put(ParameterConstant.DATA_ID_IN, dataIdList);
        map.put(ParameterConstant.DEL, DelEnum.NORMAL.getDel());
        List<ContactUserEntity> contactUserEntityList = findEntitys(map);
        contactUserDao.disbandedTeamByDataId(corpid,dataIdList);
        for (ContactUserEntity entity : contactUserEntityList){
            paasEsModel.delete(entity.getId(), corpid, IndexTypeEnum.IDX_SAAS_CONTACT_TEAM,entity.getDataId());
        }

    }

}
